package com.example.basemvvmkotlin.ui

import android.os.Bundle
import com.example.basemvvmkotlin.APP
import com.example.basemvvmkotlin.R
import com.example.basemvvmkotlin.databinding.ActivityHomeBinding
import com.example.basemvvmkotlin.net.DataList
import com.example.basemvvmkotlin.net.MyApiClient
import com.example.basemvvmkotlin.net.NewsListBean
import com.example.basemvvmkotlin.net.UserApiService
import com.example.basemvvmkotlin.vm.HomeVm
import com.example.lib_base.base.activity.BaseAppBVMActivity
import com.example.lib_base.base.util.GsonUtils
import com.example.lib_base.base.extensions.observeNonNull
import com.google.gson.JsonParser

class HomeActivity : BaseAppBVMActivity<ActivityHomeBinding, HomeVm>() {
    override fun createViewModel(): HomeVm {
        return HomeVm()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_home
    }

    lateinit var test:TestA

    override fun initialize(savedInstanceState: Bundle?) {
//        val multiStateContainer = MultiStatePage.bindMultiState(this)
//        multiStateContainer.show<ErrorView> {
//            it.setErrorIcon(R.mipmap.ic_launcher_round)
//            it.setErrorMsg("提示信息")
//            it.retry(){
//                println("------------------- 重试")
////                multiStateContainer.show<LoadingView> { loading ->
////                    loading.setLoadingMsg("")
////                }
//                multiStateContainer.show<SuccessView> {  }
////                multiStateContainer.show<EmptyState> {  }
//            }
//        }
        MyApiClient.getInstance().getService(UserApiService::class.java)
        initClick()
        addObserver()
//        test = TestB()
//        test.shuaxin()
     var json2 = """
         {
        "currentPage": 2,
        "pageSize": 20,
        "totalPage": 1786,
        "totalCount": 35704,
        "firstPage": false,
        "lastPage": false,
        "list": [
            {
                "addTime": "2021-07-15 09:27:27",
                "contentID": 152521,
                "hotPushID": 75552,
                "outLink": "",
                "shareDescription": "进入三伏后，天气开启暴走模式，我市连续3天发布高温预警信号，气温不断走高。其中，昨日竹山的最高气温达到38.9℃，不少市民惊呼“要热化了”。这种天气防中暑就成了重要问题，且看看专家们如何说。",
                "shareThumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/16263120220DFNK4ML.jpg",
                "shareTitle": "38.9℃！十堰昨日热化了！专家教你高温天气如何预防中暑",
                "shareType": "news",
                "shareUrl": "https://wap.syiptv.com/news.html?id=152521",
                "source": "十堰晚报",
                "tag": "社会",
                "thumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/16263120220DFNK4ML.jpg",
                "time": "6小时前",
                "title": "38.9℃！十堰昨日热化了！专家教你高温天气如何预防中暑",
                "type": 1,
                "views": 1,
                "visits": 1711
            },
            {
                "addTime": "2021-07-15 10:27:43",
                "contentID": 152531,
                "hotPushID": 75555,
                "outLink": "",
                "shareDescription": "近日，湖北省人力资源和社会保障厅发布湖北省2021年第二批重大劳动保障违法行为社会公布案例及拖欠农民工工资“黑名单”。",
                "shareThumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626315814D65KN0O9.jpg",
                "shareTitle": "曝光！这些企业和个人上了“黑名单”，涉十堰一人",
                "shareType": "news",
                "shareUrl": "https://wap.syiptv.com/news.html?id=152531",
                "source": "湖北发布",
                "tag": "省内",
                "thumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626315814D65KN0O9.jpg",
                "time": "5小时前",
                "title": "曝光！这些企业和个人上了“黑名单”，涉十堰一人",
                "type": 1,
                "views": 1,
                "visits": 1431
            },
            {
                "addTime": "2021-07-15 08:11:49",
                "contentID": 152507,
                "hotPushID": 75545,
                "outLink": "",
                "shareDescription": "中国经济半年报15日将正式公布。",
                "shareThumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626307411Z5NXYHJR.jpg",
                "shareTitle": "中国经济半年报今将公布 “期中卷”成绩如何？",
                "shareType": "news",
                "shareUrl": "https://wap.syiptv.com/news.html?id=152507",
                "source": "中新网",
                "tag": "时事",
                "thumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626307411Z5NXYHJR.jpg",
                "time": "7小时前",
                "title": "中国经济半年报今将公布 “期中卷”成绩如何？",
                "type": 1,
                "views": 1,
                "visits": 1371
            }
        ],
        "snapshotID": ""
    }
     """
     var json = """
      [
            {
                "addTime": "2021-07-15 09:27:27",
                "contentID": 152521,
                "hotPushID": 75552,
                "outLink": "",
                "shareDescription": "进入三伏后，天气开启暴走模式，我市连续3天发布高温预警信号，气温不断走高。其中，昨日竹山的最高气温达到38.9℃，不少市民惊呼“要热化了”。这种天气防中暑就成了重要问题，且看看专家们如何说。",
                "shareThumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/16263120220DFNK4ML.jpg",
                "shareTitle": "38.9℃！十堰昨日热化了！专家教你高温天气如何预防中暑",
                "shareType": "news",
                "shareUrl": "https://wap.syiptv.com/news.html?id=152521",
                "source": "十堰晚报",
                "tag": "社会",
                "thumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/16263120220DFNK4ML.jpg",
                "time": "6小时前",
                "title": "38.9℃！十堰昨日热化了！专家教你高温天气如何预防中暑",
                "type": 1,
                "views": 1,
                "visits": 1711
            },
            {
                "addTime": "2021-07-15 10:27:43",
                "contentID": 152531,
                "hotPushID": 75555,
                "outLink": "",
                "shareDescription": "近日，湖北省人力资源和社会保障厅发布湖北省2021年第二批重大劳动保障违法行为社会公布案例及拖欠农民工工资“黑名单”。",
                "shareThumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626315814D65KN0O9.jpg",
                "shareTitle": "曝光！这些企业和个人上了“黑名单”，涉十堰一人",
                "shareType": "news",
                "shareUrl": "https://wap.syiptv.com/news.html?id=152531",
                "source": "湖北发布",
                "tag": "省内",
                "thumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626315814D65KN0O9.jpg",
                "time": "5小时前",
                "title": "曝光！这些企业和个人上了“黑名单”，涉十堰一人",
                "type": 1,
                "views": 1,
                "visits": 1431
            },
            {
                "addTime": "2021-07-15 08:11:49",
                "contentID": 152507,
                "hotPushID": 75545,
                "outLink": "",
                "shareDescription": "中国经济半年报15日将正式公布。",
                "shareThumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626307411Z5NXYHJR.jpg",
                "shareTitle": "中国经济半年报今将公布 “期中卷”成绩如何？",
                "shareType": "news",
                "shareUrl": "https://wap.syiptv.com/news.html?id=152507",
                "source": "中新网",
                "tag": "时事",
                "thumb": "http://syiptv-media-center.oss-cn-shanghai.aliyuncs.com/other/20210715/1626307411Z5NXYHJR.jpg",
                "time": "7小时前",
                "title": "中国经济半年报今将公布 “期中卷”成绩如何？",
                "type": 1,
                "views": 1,
                "visits": 1371
            }
        ]
     """
//        try {
//            println("------------------ 列表数据  ${ JSON.parseArray<DataList>(json, DataList::class.java)}")
//
//        }catch (t:Throwable){
//            println("------------------ 数据解析异常 $t")
//        }
        var beanList = mutableListOf<DataList>()
        var Refresh = true
        try {
            var arr = JsonParser().parse(json2).asJsonArray
            println("--------------------------1 ${arr.size()}")
            arr.forEach {
                println("-------------------------2  $it")
                var data = GsonUtils.getGson().fromJson(it,DataList::class.java)
                beanList.add(data)
            }
        }catch (t:Throwable){
            println("------------------ 数据解析异常 $t")
        }

        println("------------------------ 列表数据2 ${beanList.toString()}")
    }

    private fun initClick(){
        binding.ben1.setOnClickListener {
//            APP.getInstance().build = ""
            MyApiClient.getInstance().build = "1124122"
            viewModel.getNewsListBean(1)
        }

        binding.btn2.setOnClickListener {
           viewModel.getInfo()
        }
    }

    private fun addObserver() {
        if ( viewModel.newsList.value is NewsListBean) {
            println("_____________________ 是列表数据")
        }else {
            println("_____________________ 不是列表数据   ${viewModel.newsList.value}")
        }

        viewModel.newsList.observeNonNull(this){
            binding.tvContent.text = it.list[0].toString()
            if ( viewModel.newsList.value is NewsListBean) {
                println("_____________________ 是列表数据")
            }else {
                println("_____________________ 不是列表数据   ${viewModel.newsList.value}")
            }
        }

        viewModel.info.observeNonNull(this){
            binding.tvContent.text = it.toString()
        }
    }

}

open class TestA {
    fun shuaxin(){
        println("---------------- 刷新")
    }
}

class TestB :TestA(){
    fun addData(){
        println("---------------- 添加数据")
    }
}